//
//  WKPopCancelView.m
//  danpin
//
//  Created by chuangjia on 14/11/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "WKPopCancelView.h"
#import "RadioBox.h"
#import "RadioGroup.h"
#import "BaseCell.h"
#define WKRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
//按钮颜色
#define OKBUTTON_BGCOLOR AlertRGB(0x1666C8)
#define CANCELBUTTON_BGCOLOR AlertRGB(0xf8f8f8)
//按钮起始tag
#define TAG 100

#define SCREEN_Width   [UIScreen mainScreen].bounds.size.width
#define SCREEN_Height   [UIScreen mainScreen].bounds.size.height
@interface WKPopCancelView ()
{
    
}
@property(nonatomic,strong) UILabel *title;
@property(nonatomic,strong) UILabel *subtitle;
@property(nonatomic,strong) NSArray *controls;
@end
@implementation WKPopCancelView
@synthesize title=_title;
@synthesize subtitle=_subtitle;


+ (instancetype)showAlertViewWithTitle:(NSString *)title subTitle:(NSString*)subtitle callBlock:(callBack)callBack
{
    [[self shared] addTitle:title detail:subtitle];
    [[self shared] setClickBlock:nil];//释放掉之前的Block
    [[self shared] setClickBlock:callBack];
    [[self shared] setHidden:NO];//设置为不隐藏
    return  [self shared];
}
//单例
+ (instancetype)shared
{
    static dispatch_once_t once = 0;
    static WKPopCancelView *alert;
    dispatch_once(&once, ^{
        alert = [[WKPopCancelView alloc] init];
    });
    return alert;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = (CGRect) {{0.f,0.f}, [[UIScreen mainScreen] bounds].size};
        self.alpha = 1;
        [self setBackgroundColor:[UIColor clearColor]];
        self.hidden = NO;//不隐藏
        self.windowLevel = 1000;
        [self initui];
    }
    
    return self;
}

-(void)dealloc
{
    self.subtitle=nil;
    self.title=nil;
}
-(void)initui
{
    UIImage *image =[UIImage imageNamed:@"windows_bg.png"];
    UIView *sview =[[UIView alloc] initWithFrame:CGRectMake(0, 0,widthAuto(image.size.width) ,hightAuto(image.size.height))];
    sview.backgroundColor=[UIColor clearColor];
    sview.center=self.center;
    //
    UIImageView *imageview =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, widthAuto(image.size.width), hightAuto(image.size.height))];
    imageview.image=image;
    [sview addSubview:imageview];
    imageview=nil;
    
    //代码实现
    RadioBox *radiobox1 = [[RadioBox alloc] initWithFrame:CGRectMake(40, 40, widthAuto(image.size.width)-40, 30)];
    RadioBox *radiobox2 = [[RadioBox alloc] initWithFrame:CGRectMake(40, 90, widthAuto(image.size.width)-40, 30)];
    RadioBox *radiobox3 = [[RadioBox alloc] initWithFrame:CGRectMake(40, 140, widthAuto(image.size.width)-40, 30)];
    RadioBox *radiobox4 = [[RadioBox alloc] initWithFrame:CGRectMake(40, 190, widthAuto(image.size.width)-40, 30)];
    
    
    radiobox1.textColor = [UIColor whiteColor];
    radiobox2.textColor = [UIColor whiteColor];
    radiobox3.textColor = [UIColor whiteColor];
    radiobox4.textColor = [UIColor whiteColor];
    
    radiobox1.text = @"现在不想买";
    radiobox2.text = @"收货人有误";
    radiobox3.text = @"收货时间过长";
    radiobox4.text = @"无法支付";
 
    
    radiobox1.value = 0;
    radiobox2.value = 1;
    radiobox3.value = 2;
    radiobox4.value = 2;
    
    _controls = [NSArray arrayWithObjects:radiobox1,radiobox2,radiobox3,radiobox4, nil];
    
    RadioGroup *radioGroup1 = [[RadioGroup alloc] initWithFrame:CGRectMake(10, 10, widthAuto(image.size.width)-20, 226) WithControl:_controls];

    radioGroup1.backgroundColor = [UIColor clearColor];
    radioGroup1.textFont = [UIFont systemFontOfSize:14.0];
    radioGroup1.selectValue = 0;
    
    [sview addSubview:radioGroup1];
    
  
    //
    UIButton *cacelbtn =[UIButton buttonWithType:UIButtonTypeCustom];
    cacelbtn.tag=0;
    cacelbtn.backgroundColor =[UIColor clearColor];
    [cacelbtn setTitleColor:UIColorFromRGB(0x808080) forState:UIControlStateNormal];
    cacelbtn.frame =CGRectMake((widthAuto(image.size.width)/2-widthAuto(45))/2,  hightAuto(image.size.height)-hightAuto(21)-32,widthAuto(45) ,hightAuto(21) );
    cacelbtn.titleLabel.font =FONT(15);
    [cacelbtn setTitle:@"取消" forState:UIControlStateNormal];
    [cacelbtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [sview addSubview:cacelbtn];
    //
    UIButton *okBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    okBtn.backgroundColor =[UIColor clearColor];
    okBtn.tag=1;
    [okBtn setTitleColor:UIColorFromRGB(0x1666C8) forState:UIControlStateNormal];
    okBtn.frame =CGRectMake(widthAuto(image.size.width)/2+widthAuto(60)/2,  hightAuto(image.size.height)-hightAuto(21)-32,widthAuto(60) ,hightAuto(21) );
    okBtn.titleLabel.font =FONT(15);
    [okBtn setTitle:@"确定" forState:UIControlStateNormal];
    [okBtn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [sview addSubview:okBtn];
    
    //
    [self addSubview:sview];
    sview=nil;
}
- (void)addTitle:(NSString *)title detail:(NSString *)detail
{
    
    _title.text  = title;
    _subtitle.text =detail;
}

- (void)buttonClick:(UIButton *)sender
{
    int index =0 ;
    for (UIView *control in self.controls) {
        if ([control isKindOfClass:[RadioBox class]]) {
            RadioBox* control1 =  (RadioBox*)control;
            index =(int)control1.value;
        }
    }
    NSString *text =@"";
    switch (index) {
        case 0:
        {
            text= @"现在不想买";
          
        }
            break;
        case 1:
        {
            text = @"收货人有误";
        }
            break;
        case 2:
        {
            text = @"收货时间过长";
        }
            break;
        case 3:
        {
            text = @"无法支付";
        }
            break;
        default:
            break;
    }
    self.clickBlock(sender.tag,text);
}

@end